<script>
import {nanoid} from 'nanoid';

export default {
  name: 'MyPerson',
  data() {
    return {
      title: ""
    }
  },
  computed: {
    //自己手动通过$store去获取自己组件需要的personList值
    personList(){
      return this.$store.state.personOption.personList;
    },
    //自己手动通过$store去获取count中的sum值
    sum(){
      return this.$store.state.countOption.sum;
    },
    //自己手动通过$store去获取state中的personList的title的值拼接成字符串响应给页面展示
    getTitleStr(){
      return this.$store.getters["personOption/getEnglishName"]
    }
  },
  methods: {
    //自己手动通过$store去提交mutations
    add() {
      const newPerson = {
        id: nanoid(),
        title: this.title
      };
      this.$store.commit('personOption/ADDPERSON', newPerson)
      this.title = "";
    },
    //自己手动通过$store去提交actions
    addPersonWang(){
      this.$store.dispatch('personOption/addPersonWang',this.title)
    }
  }
}
</script>

<template>
  <div>
    <h3 style="color: green">count组件的求和结果为:{{sum}}</h3>
    <input type="text" v-model="title" placeholder="请输入姓名">
    <button @click="add">添加人员</button>
    <button @click="addPersonWang">添加王先生</button>
    <h3>获取输入的拼音为:{{getTitleStr}}</h3>
    <ul>
      <li v-for="item in personList" :key="item.id">
        {{item.title}}
      </li>
    </ul>
  </div>
</template>